/* -*-c++-*- IfcGears - www.ifcgears.com  - Copyright (C) 2011 Fabian Gerold
 *
 * This library is open source and may be redistributed and/or modified under  
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * OpenSceneGraph Public License for more details.
*/

#pragma once

#include <QObject>
#include <osgGA/GUIEventHandler>
#include "ifcgears/model/shared_ptr.h"

class IfcGearsModel;
class IfcGearsEntity;
class ReaderWriterIFC;
class CommandManager;
class ViewController;
namespace osg { class Material; }

struct selectedEntity 
{
	shared_ptr<IfcGearsEntity> entity;
	osg::ref_ptr<osg::StateSet> original_stateset;
	osg::ref_ptr<osg::Group> osg_group;
};

class IfcGearsSystem : public QObject, public osgGA::GUIEventHandler
{
	Q_OBJECT

public:
	IfcGearsSystem();
	~IfcGearsSystem();

	bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa);
	bool intersectModel( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa, bool click, bool doubleclick );
	void loadIfcFile( std::string& path_in, bool optimize = false );
	void writeIfcFile( std::string& path_out );
	
	shared_ptr<IfcGearsModel>	getIfcModel()	{ return m_ifc_model; }
	shared_ptr<ReaderWriterIFC> getReaderWriterIFC() {	return m_rw; }
	shared_ptr<CommandManager>	getCommandManager() { return m_command_manager; }
	shared_ptr<ViewController>	getViewController() { return m_view_controller; }

	void setObjectSelected( shared_ptr<IfcGearsEntity> object, bool selected, osg::Group* node = 0 );
	const std::map<int, shared_ptr<selectedEntity> >& getSelectedObjects() { return m_map_selected; }
	void clearSelection();
	
private:
	shared_ptr<IfcGearsModel>			m_ifc_model;
	shared_ptr<ReaderWriterIFC>			m_rw;
	shared_ptr<CommandManager>			m_command_manager;
	shared_ptr<ViewController>			m_view_controller;
	bool								m_drag;
	std::map<int, shared_ptr<selectedEntity> > m_map_selected;
	osg::ref_ptr<const osgGA::GUIEventAdapter> m_ga_t0;

signals:
	void signalObjectSelected( shared_ptr<IfcGearsEntity> object );
	void signalObjectUnSelected( shared_ptr<IfcGearsEntity> object );
};
